package com.fingard.dsp.bank.directbank.chinapay05;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import com.chinapay.secss.SecssConstants;
import com.chinapay.secss.SecssUtil;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IOtherElse;
import com.fingard.dsp.bank.directbank.chinapay01.util.AcpService;
import com.fingard.dsp.bank.reqrespobj.otherelse.ReqOtherElse;
import com.fingard.dsp.bank.reqrespobj.otherelse.RetAtsOtherElse;

import org.apache.commons.lang.StringUtils;

/**
 * 异步通知验签
 * 
 * @author Cruise
 * @date 2018年9月27日
 */
public class ChinaPay05OtherElse extends ChinaPay05Base implements IOtherElse {
	@Override
	public RetAtsOtherElse d_else(ReqOtherElse p_reqElse) throws Exception {
		String transType = p_reqElse.reqHeader.transType;
		if ("VerifySign".equals(transType)) {
			return verifyGateway(p_reqElse);

		} else {
			RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
			retAts.retHashMap = new HashMap<String, String>();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", "未知的TransType：" + transType);
			return retAts;
		}
	}

	/**
	 * 网关支付验签
	 * 
	 * @param p_reqElse
	 * @return
	 */
	public RetAtsOtherElse verifyGateway(ReqOtherElse p_reqElse) {
		RetAtsOtherElse retAts = new RetAtsOtherElse(p_reqElse.reqHeader);
		retAts.retHashMap = new HashMap<String, String>();

		try {
			String ownAct = "";
			if (p_reqElse.reqHashMap.containsKey("OurBankAccount")) {
				ownAct = p_reqElse.reqHashMap.get("OurBankAccount");
			}
			
			String plain = "";
			if (p_reqElse.reqHashMap.containsKey("Plain")) {
				plain = p_reqElse.reqHashMap.get("Plain");
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "缺失验签原始串!");
				return retAts;
			}
			
			ActSetItem actItem = getBankConfig().getActSetItem(ownAct, "gateway");
			if (actItem != null) {

				SecssUtil secssUtil = new SecssUtil();
				Properties properties = new Properties();
				properties.setProperty("verify.file", actItem.oppCertFilePath);
				properties.setProperty("sign.file", actItem.ownKeyStorePath);
				properties.setProperty("sign.file.password", actItem.ownKeyPassword);
				properties.setProperty("sign.cert.type", "PKCS12");
				properties.setProperty("sign.invalid.fields", "Signature");
				properties.setProperty("signature.field", "Signature");
				secssUtil.init(properties);

				Map<String, String> tmpSignMap = convertToMap(plain);
				String sign = tmpSignMap.get("Signature");
				if (StringUtils.isNotEmpty(sign)) {
					secssUtil.verify(tmpSignMap);
				} else {
					retAts.retHashMap.put("RespCode", "-1");
					retAts.retHashMap.put("RespInfo", "返回结果无签名");
				}


				if (SecssConstants.SUCCESS.equals(secssUtil.getErrCode())) {
					retAts.retHashMap.put("RespCode", "0");
					retAts.retHashMap.put("RespInfo", "验签通过");
				} else {
					retAts.retHashMap.put("RespCode", "-1");
					retAts.retHashMap.put("RespInfo", "验签失败");
				}
			} else {
				retAts.retHashMap.put("RespCode", "-1");
				retAts.retHashMap.put("RespInfo", "未找到账号:[" + ownAct + "]商户配置信息!");
			}
		} catch (Exception ex) {
			ex.printStackTrace();
			retAts.retHashMap.put("RespCode", "-1");
			retAts.retHashMap.put("RespInfo", ex.getMessage());
			WriteBankLogLn(ex);
		}

		return retAts;
	}
	

}
